/*
 * Copyright (c)  2008 Sebastien Tremblay
 *
 * id3-tag-builder - helpers.StringHelper.java
 *
 * All source and documentation is copyrighted by Sebastien Tremblay
 * (seb at ryders dot net) and made available under the Apache License 2.0.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id: StringHelper.java 30 2010-01-08 04:01:06Z ryders $
 */
package com.empireone.id3tagbuilder.helpers;

/**
 * The <code>StringHelper</code> contains a series of 'toolkit methods' around
 * <see>java.lang.String</see>.
 *
 * @author Sebastien Tremblay
 */
public class StringHelper {

    /**
     *
     * @param msg
     * @param params
     * @return
     */
    public static String substitute(String msg, String params) {
        return substitute(msg, params.split("\\|"));
    }

    /**
     *
     * @param msg
     * @param params
     * @return
     */
    public static String substitute(String msg, String[] params) {

        for (int i = 0; i < params.length; i++) {
            msg = msg.replace("{" + i + "}", params[i]);
        }

        return msg;
    }

    /**
     *
     * @param fullPath
     * @param placeholder
     * @return
     */
    public static String relPath(String fullPath, String placeholder) {
        return fullPath.substring(fullPath.indexOf(placeholder));
    }

    /**
     *
     * @param pieces
     * @param delim
     * @return
     */
    public static String join(String[] pieces, String delim) {
        String res = "";
        for (int i = 0; i < pieces.length; i++) {
            res += pieces[i] + (i < pieces.length ? delim : "");
        }
        return res;
    }

    /**
     *
     * @param value
     * @return
     */
    public static int wordsToInt(String value) {
        return wordsToIntOnes(value);
    }

    private static int wordsToIntOnes(String value) {
        int res = 0;
        if (value.endsWith("one")) {
            res += 1;
        }
        if (value.endsWith("two")) {
            res += 2;
        }
        if (value.endsWith("three")) {
            res += 3;
        }
        if (value.endsWith("four")) {
            res += 4;
        }
        if (value.endsWith("five")) {
            res += 5;
        }
        if (value.endsWith("six")) {
            res += 6;
        }
        if (value.endsWith("seven")) {
            res += 7;
        }
        if (value.endsWith("eight")) {
            res += 8;
        }
        if (value.endsWith("nine")) {
            res += 9;
        }
        if (value.endsWith("ten")) {
            res += 10;
        }
        if (value.endsWith("eleven")) {
            res += 11;
        }
        if (value.endsWith("twelve")) {
            res += 12;
        }
        if (value.endsWith("thirteen")) {
            res += 13;
        }
        if (value.endsWith("fourteen")) {
            res += 14;
        }
        if (value.endsWith("fifteen")) {
            res += 15;
        }
        if (value.endsWith("sixteen")) {
            res += 16;
        }
        if (value.endsWith("seventeen")) {
            res += 17;
        }
        if (value.endsWith("eighteen")) {
            res += 18;
        }
        if (value.endsWith("nineteen")) {
            res += 19;
        }
        if (value.endsWith("twenty")) {
            res += 20;
        }
        return res;
    }
}
